/*
 * @Author: Black / 李来然
 * @Date: 2022-01-24 16:48:16
 * @LastEditors: Black / 李来然
 * @LastEditTime: 2022-01-25 16:44:27
 * @Description: 条形码模块儿
 */
import { ModsInstance } from "@/func/createMod";
import { BaseIO } from "./baseIO";
import { getModeFuncRet, RetResult } from "../tools/modHelper";

export interface BcrState {}
export interface BcrCapability {}

type ReadBarcodeRet =
	| "BarcodeRead"
	| "FatalError"
	| "DeviceError"
	| "ReadBarcodeCancelled"
	| "Timeout";

type CancelReadBarcodeRet =
	| "ReadBarcodeCancelled"
	| "FatalError"
	| "DeviceError";

type ResetRet = "ResetComplete" | "FatalError" | "DeviceError";

export class Bcr extends BaseIO<BcrState, BcrCapability> {
	constructor(modName: keyof ModsInstance, mod: any) {
		super(modName, mod);
	}

	/**
	 * 读条码
	 * @param {number} lTime 超时时间
	 * @returns {Promise<json>} - {mod:this.name, ret: '', param: arg}
	 * @return {String} return.mod 模块名
	 * @return {String} return.ret 事件 <br> BarcodeRead - 条码读取成功 <br> Timeout - 超时 <br> FatalError - 失败 <br> DeviceError - 设备故障 <br> NotSupport - 不支持 <br>
	 * @return {Array} return.param - 事件参数 <br> param[0] - 条码值，示例: 903485949
	 */
	ReadBarcode(lTime: number) {
		Trace(EC_INFO, `-->Bcr:ReadBarcode`);
		this.setCurrentMethod("ReadBarcode");
		this.removeMethodBindEvt("ReadBarcode");
		return new Promise<RetResult<ReadBarcodeRet>>((resolve, reject) => {
			const BarcodeRead = (...args: any[]) => {
				// let arg = ["BarcodeRead"].concat(args.slice(0));
				Trace(EC_INFO, `<--${this.modName}:ReadBarcode BarcodeRead`);
				resolve(getModeFuncRet(this.modName, "BarcodeRead", args));
			};
			const FatalError = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ReadBarcode FatalError`);
				resolve(getModeFuncRet(this.modName, "FatalError", args));
			};
			const DeviceError = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ReadBarcode DeviceError`);
				resolve(getModeFuncRet(this.modName, "DeviceError", args));
			};
			const ReadBarcodeCancelled = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ReadBarcode ReadBarcodeCancelled`);
				resolve(getModeFuncRet(this.modName, "ReadBarcodeCancelled", args));
			};
			const Timeout = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ReadBarcode Timeout`);
				resolve(getModeFuncRet(this.modName, "Timeout", args));
			};

			this.add("BarcodeRead", BarcodeRead);
			this.add("FatalError", FatalError);
			this.add("DeviceError", DeviceError);
			this.add("ReadBarcodeCancelled", ReadBarcodeCancelled);
			this.add("Timeout", Timeout);

			this.mod.ReadBarcode(lTime, (ret: string) => {
				if (parseInt(ret) !== 0) {
					Trace(EC_INFO, `<--${this.modName}:ReadBarcode ret=${ret}`);
					resolve(getModeFuncRet(this.modName, "FatalError"));
				}
			});
		}).then((value) => {
			this.removeMethodBindEvt("ReadBarcode");
			return value;
		});
	}

	/**
	 * 取消读条码
	 * @returns {Promise<json>} - {mod:this.modName, ret: '', param: arg}
	 * @return {String} return.mod 模块名
	 * @return {String} return.ret 事件 <br> ReadBarcodeCancelled - 取消成功 <br> FatalError - 失败 <br> DeviceError - 设备故障 <br>
	 * @return {Array} return.param - 事件参数 无
	 */
	CancelReadBarcode() {
		Trace(EC_INFO, `-->Bcr:CancelReadBarcode`);
		this.setCurrentMethod("CancelReadBarcode");
		this.removeMethodBindEvt("CancelReadBarcode");
		return new Promise<RetResult<CancelReadBarcodeRet>>((resolve, reject) => {
			const ReadBarcodeCancelled = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:CancelReadBarcode ReadBarcodeCancelled");
				resolve(getModeFuncRet(this.modName, "ReadBarcodeCancelled", args));
			};
			const FatalError = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:CancelReadBarcode FatalError");
				resolve(getModeFuncRet(this.modName, "FatalError", args));
			};
			const DeviceError = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:CancelReadBarcode DeviceError");
				resolve(getModeFuncRet(this.modName, "DeviceError", args));
			};

			this.add("ReadBarcodeCancelled", ReadBarcodeCancelled);
			this.add("FatalError", FatalError);
			this.add("DeviceError", DeviceError);

			this.mod.CancelReadBarcode((ret: string) => {
				if (parseInt(ret) !== 0) {
					Trace(EC_INFO, "<--Bcr:CancelReadBarcode ret=" + ret);
					resolve(getModeFuncRet(this.modName, "FatalError"));
				}
			});
		}).then((value) => {
			this.removeMethodBindEvt("CancelReadBarcode");
			return value;
		});
	}

	/**
	 * 复位
	 * @returns {Promise<json>} - {mod:this.modName, ret: '', param: arg}
	 * @return {String} return.mod 模块名
	 * @return {String} return.ret 事件 <br> BcrResetComplete - 复位成功 <br> FatalError - 失败 <br> DeviceError - 设备故障 <br>
	 * @return {Array} return.param - 事件参数 param[0] - 条码值，示例: 903485949
	 */
	Reset() {
		Trace(EC_INFO, "-->Bcr:Reset");
		this.setCurrentMethod("Reset");
		this.removeMethodBindEvt("Reset");
		return new Promise<RetResult<ResetRet>>((resolve, reject) => {
			const ResetComplete = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:Reset ResetComplete");
				resolve(getModeFuncRet(this.modName, "ResetComplete", args));
			};
			const FatalError = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:Reset FatalError");
				resolve(getModeFuncRet(this.modName, "FatalError", args));
			};
			const DeviceError = (...args: any[]) => {
				Trace(EC_INFO, "<--Bcr:Reset DeviceError");
				resolve(getModeFuncRet(this.modName, "DeviceError", args));
			};

			this.add("ResetComplete", ResetComplete);
			this.add("FatalError", FatalError);
			this.add("DeviceError", DeviceError);

			this.mod.Reset((ret: string) => {
				if (parseInt(ret) !== 0) {
					Trace(EC_INFO, "<--Bcr:Reset ret=" + ret);
					resolve(getModeFuncRet(this.modName, "FatalError"));
				}
			});
		}).then((value) => {
			this.removeMethodBindEvt("Reset");
			return value;
		});
	}
}
